<?php

namespace Common\Model;

use Think\Model;

class DriverModel extends Model {
    /**
     * 司机钱包变动
     * @param type $param
     */
    public function driverChangeWallet($driver_id=0,$change_type=0,$amount=0,$order_id=0) {
        $total_amount = $this->where(array('driver_id'=>$driver_id))->getField('driver_total_amount');
        switch ($change_type) {
            case 1:     //提现
                $amount = -$amount;
                $add_subtract = 2;
                break;
            case 5:     //订单收入
                $amount = $amount;
                $add_subtract = 1;
                break;
        }
        D()->startTrans();
        $companyRes = $this->where(array('driver_id'=>$driver_id))->save(array('driver_total_amount'=>['exp','driver_total_amount+('.$amount.')']));
        $new_total_amount = $this->where(array('driver_id'=>$driver_id))->getField('driver_total_amount');
        $data['driver_id'] = $driver_id;
        $data['change_type'] = $change_type;
        $data['add_subtract'] = $add_subtract;
        $data['change_price'] = $amount;
        $data['wallet_balance'] = $new_total_amount;
        $data['order_id'] = $order_id;
        $walletRes = M('driver_wallet_change')->add($data);
        if($companyRes && $walletRes){
            D()->commit();
            return TRUE;
        }else{
            D()->rollback();
            return FALSE;
        }
        
    }
}
